package com.itheima.service.db.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public Long save(User user) {
        //密码加密
        String md5Pwd = SecureUtil.md5(user.getPassword());

        user.setPassword(md5Pwd);

        userMapper.insert(user);

        return user.getId();
    }

    @Override
    public User findByPhone(String phone) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(User::getMobile, phone);

        return userMapper.selectOne(wrapper);
    }

    @Override
    public int  update(User user, Long id) {

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId, id);
        System.err.println(user);
        int i = userMapper.update(user,lambdaQueryWrapper);
        System.err.println(user);
        return i;
    }

}
